package org.janon.httpproxy.client;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.janon.httpproxy.Log;
import org.janon.httpproxy.LogList;
import org.janon.httpproxy.Monitor;

public class ClientUI extends JDialog
{
	private static final long serialVersionUID = 1L;

	private JTextField sAddr;
	private JTextField sPort;
	private JTextField cPort;
	private Thread thread;
	private Client client;
	private LogList logList;

	public ClientUI()
	{
		super();

		this.getContentPane().setLayout(new BorderLayout());

		JPanel settingPanel = new JPanel();
		this.getContentPane().add(settingPanel, BorderLayout.NORTH);
		settingPanel.setLayout(new GridLayout(2, 4));
		settingPanel.add(new JLabel("Server Addr:"));
		sAddr = new JTextField();
		settingPanel.add(sAddr);
		settingPanel.add(new JLabel("Server Port:"));
		sPort = new JTextField();
		settingPanel.add(sPort);
		settingPanel.add(new JLabel("Client Port:"));
		cPort = new JTextField();
		settingPanel.add(cPort);
		settingPanel.add(new JLabel(""));
		JButton startBtn = new JButton("Start");
		settingPanel.add(startBtn);
		startBtn.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e)
			{
				JButton btn = ((JButton) e.getSource());
				if (btn.getText().compareToIgnoreCase("Start") == 0)
				{
					boolean flag = true;
					String sAddrText = sAddr.getText();
					if (sAddrText.length() == 0)
						flag = false;
					int sPortInt = -1;
					try
					{
						sPortInt = Integer.parseInt(sPort.getText());
					}
					catch (Exception ex)
					{
						sPortInt = -1;
					}
					if (sPortInt <= 0)
						flag = false;

					int cPortInt = -1;
					try
					{
						cPortInt = Integer.parseInt(cPort.getText());
					}
					catch (Exception ex)
					{
						cPortInt = -1;
					}
					if (cPortInt <= 0)
						flag = false;

					if (flag)
					{
						client = new Client(sAddrText, sPortInt, cPortInt);
						thread = new Thread(client);
						thread.start();
						btn.setText("Stop");
						sAddr.setEditable(false);
						sPort.setEditable(false);
						cPort.setEditable(false);
					}
				}
				else if (btn.getText().compareToIgnoreCase("Stop") == 0)
				{
					client.stop();
					thread.interrupt();
					btn.setText("Start");
					sAddr.setEditable(true);
					sPort.setEditable(true);
					cPort.setEditable(true);
					thread = null;
					client = null;
				}
			}
		});
		logList = new LogList();
		this.getContentPane().add(new JScrollPane(logList.getList()), BorderLayout.CENTER);
		Log.getInstance().setWriteToScream(false);
		Log.getInstance().addRecordLog(logList);

		JLabel label = new JLabel(" ");
		this.getContentPane().add(label, BorderLayout.SOUTH);
		Monitor.getInstance().setLabel(label);

		this.setSize(300, 200);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);

	}

	@SuppressWarnings("deprecation")
	public void dispose()
	{
		if (client != null)
			client.stop();
		if (thread != null)
			thread.interrupt();
		sAddr.setEditable(true);
		sPort.setEditable(true);
		cPort.setEditable(true);
		thread = null;
		client = null;
		super.dispose();
	}

	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new ClientUI();
		}
		catch (Exception e)
		{
		}
	}
}
